//editRoleCtrl

app.controller('memberCtrl', function($element, $scope, $state, rest, user, uploadConf, tipService, $window, $rootScope, $compile, ngDialog, $ocLazyLoad, uiLoad,$http,decoJavaUrl) {
	$scope.iNowData = angular.copy($scope.ngDialogData, {});
    var token = $window.localStorage.getItem("token");
    $scope.init = function(){
    	
    	//请求select角色列表
    	rest.decoJava({
    		type: 'post',
	        url: '/tenancy-sys/admin/role/all/list',
	        data: '',
	        beforeSend: function(request){
				request.setRequestHeader("token", token);
			},
	        success: function(data){             
	          	$scope.myDatas = data.result;
	          	angular.forEach($scope.myDatas,function(data,index,array){
	          		if(data.id == $scope.iNowData.id){
	          			$scope.$apply(function(){
	          				$scope.itemRole = data; //遍历出来的select默认选中第一个
	          	 			$scope.changeRole($scope.itemRole); //主动执行第一次
	          	 		})
	          		}
	          	})
	        }
	    })
    	
    };
    $scope.init();
    
    //切换角色
    $scope.changeRole = function(item){
    	//$scope.changeRoleId = item;
    	//根据角色去请求对应的成员列表
    	rest.decoJava({
    		type: 'post',
	        url: '/tenancy-sys/admin/role/detail/'+item.id,
	        data: '',
	        beforeSend: function(request){
				request.setRequestHeader("token", token);
			},
	        success: function(data){             
	          	console.log(data);
	          	$scope.roles = data.result.users;
	          	$scope.$apply();
	        }
	    })
    	
    }
    
    //移除成员
    $scope.removeMember = function(x){
    	var that = this;
    	rest.decoJava({
    		type: 'post',
	        url: '/tenancy-sys/admin/role/remove/user',
	        data: {roleId: $scope.itemRole.id, userId:  x.id},
	        beforeSend: function(request){
				request.setRequestHeader("token", token);
			},
	        success: function(data){             
	          	if(data.code == "0000"){
	          		layer.msg(data.message, {
			        	offset:'t',
			        	icon: 1
					});
	          	}
	          	that.removeM = true;
	          	$scope.$apply();
	        }
	    })
    }
    
    //弹出添加成员窗口
    $scope.addRoleModal = function(e,iDisplayIndex){
    	//var btn = $(e.target);
        ngDialog.openConfirm({
          template: 'js/module/decorate/addRole.html',
          resolve: {
            load: function() {
                return $ocLazyLoad.load('angularFileUpload').then(
                    function(){
                        return uiLoad.load(['js/module/decorate/addRole.js'])
                    });
            }
          },
          data:$scope.changeRoleId,
          scope: $scope,
          width: 1200,
          // height: 700
        }).then(function(data) {
              $scope.changeRole($scope.itemRole); //添加成功返回的时候执行一次请求达到刷新目的
        }, function(reason) {
              $scope.changeRole($scope.itemRole); //添加成功返回的时候执行一次请求达到刷新目的
        });
    }
    
});
